package org.zjx.app.action.login;


import org.zjx.analysis.analyze.login.LoginCreateAnalysis;
import org.zjx.command.entity.Race;
import org.zjx.command.entity.Sects;
import org.zjx.command.state.AccountState;
import org.zjx.window.action.Action;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;

public class LoginCreateAction extends Action { // 在需要的时候才进行详细分析
    protected LoginCreateAnalysis loginCreateAnalysis;

    public LoginCreateAction(IWindow targetWindow) {
        super(targetWindow, false);
        loginCreateAnalysis = new LoginCreateAnalysis();
        loginCreateAnalysis.analyze(targetWindow.getScreenshot());
    }

    @Override
    protected void doExecute() throws Exception {
        AccountState accountState = targetWindow.getState().getState(AccountState.class);
        Sects sects = accountState.getSect();
        Race race = accountState.getRace();

        boolean chooseCategory = loginCreateAnalysis.isCategoryChoose(targetWindow.getScreenshot(), race);
        if (!chooseCategory) {
            Rectangle categoryHandoffButtonRectangle = loginCreateAnalysis.getCategoryHandoffRectangle();
            targetWindow.actions().clickAt(categoryHandoffButtonRectangle.getCenterX(), categoryHandoffButtonRectangle.getCenterY()).delay(500);
        } else {
            boolean chooseRace = loginCreateAnalysis.isRaceChoose(targetWindow.getScreenshot(), race);
            if (!chooseRace) {
                Rectangle raceHandoffButtonRectangle = loginCreateAnalysis.getRaceHandoffRectangle();
                targetWindow.actions().clickAt(raceHandoffButtonRectangle.getCenterX(), raceHandoffButtonRectangle.getCenterY());
            } else {
                String name = accountState.getUsername();
                if (loginCreateAnalysis.haveName(targetWindow.getScreenshot())) {
                    targetWindow.actions().keyPress(KeyEvent.VK_BACK_SPACE).delay(50)
                            .keyPress(KeyEvent.VK_BACK_SPACE).delay(50).keyPress(KeyEvent.VK_BACK_SPACE).delay(50);
                }else {
                    Rectangle nextButtonRectangle = loginCreateAnalysis.getNextButtonRectangle();
                    targetWindow.actions().input(name).delay(200).
                    moveTo(nextButtonRectangle.getCenterX(), nextButtonRectangle.getCenterY()).delay(1500);
                }
            }
        }
        targetWindow.execute(WindowOperation.delay(500));

    }
}
